package com.darren.center.design.patterns.controller;

import com.darren.center.design.patterns.command.Dispatcher;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * <h3>design-patterns</h3>
 * <p></p>
 *
 * @author : Darren
 * @date : 2021年06月14日 20:08:45
 **/
@Controller
public class OpenController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(OpenController.class);
    @Resource(name = "open-action-Dispatcher")
    private Dispatcher dispatcher;


    /**
     * 处理医院回调信息
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/health", method = RequestMethod.POST)
    public void health(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.setCharacterEncoding("UTF-8");
            BufferedReader reader = request.getReader();
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String respXml = dispatcher.doAction(sb.toString(), getContext(request));
            if (StringUtils.isBlank(respXml)) {
                response.setStatus(403);
                return;
            }
            PrintWriter pw = response.getWriter();
            pw.print(respXml);
            pw.flush();
        } catch (IOException e) {
            logger.info(">>==getGzhmsg exception" + e.getMessage());
        }
    }
}

